<?php
App::uses('AppModel', 'Model');
class Category extends AppModel {

	public $name = 'Category';
	public $useTable = 'categories_multi';

	/**
	 * Get category
	 *
	 * @param string $cat_type
	 * @throws NotFoundException
	 * @return mixed
	 */
	public function getListCategoryType($cat_type = null) {
		try{
			$results = $this->query(
					"SELECT id,cat_name,cat_order,cat_rewrite,cat_type,cat_parent_id
					FROM categories_multi
					WHERE cat_type IN (" . $cat_type . ") AND cat_active = 1
					ORDER BY cat_type ASC, cat_order ASC");
			return  $results;

		} catch (Exception $e) {
			throw new NotFoundException(__('Invalid category'));
		}

	}

	/**
	 * Get level category parent
	 * @param number $id
	 * @return number $cat_level
	 */
	public function getLevelCategoryParent($id = null){
		$query = array(
				'conditions' => array(
					'id' => $id,
					'cat_active' => 1
				),
				'fields' => 'cat_level,cat_has_child',
		);
		try{
			$cat_parent = $this->find('first', $query);
			return $cat_parent['Category']['cat_level'];

		} catch (Exception $e) {
			throw new NotFoundException(__('Invalid category'));
		}
	}

	/**
	 * Get news category display home page
	 * @return array
	 */
	public function getNewsDisplayHome(){
		//get all categroy have cat_home=1
		$home_category = $this->query(
					"SELECT id,cat_name,cat_rewrite,cat_parent_id
					FROM categories_multi
					WHERE cat_home = 1 AND cat_active = 1
					ORDER BY cat_type ASC, cat_order ASC"
		);
		foreach ($home_category as $key => &$cat){
			$list_id = $cat['categories_multi']['id'];
			$cat_child = $this->query(
					"SELECT id
					FROM categories_multi
					WHERE cat_parent_id = ". $cat['categories_multi']['id']
			);
			if (!empty($cat_child)){
				foreach ($cat_child as $child){
					$list_id .= "," . implode(',', $child['categories_multi']);
				}
			}
			//get all news have category_id = $list_id
			$news = $this->query(
					"SELECT id,name,brief,contents,new_picture,new_rewrite
					FROM news
					WHERE active = 1 AND del_flg = 0
					AND category_id IN (" . $list_id .")
					ORDER BY create_time DESC LIMIT 6"
			);

			if(isset($news[0]) && !empty($news[0])){
				$cat['top_news'] = reset($news[0]); unset($news[0]);
			}

			if(!empty($news)){
				$cat['news_category'] = $news;
			}
		}

		return $home_category;
	}

	public function getCategoryId($new_rewrite = null){
		try{
			$category = $this->query(
				"SELECT id,cat_name,cat_parent_id,cat_title,cat_keyword,cat_description
				FROM categories_multi
				WHERE cat_md5 = '" . $new_rewrite . "' AND cat_active = 1"
			);
			$category = reset($category);
			$list_id = array($category['categories_multi']['id']);

			if ($category['categories_multi']['cat_parent_id'] == 0) {
				$cat_child = $this->query(
						"SELECT id
						FROM categories_multi
						WHERE cat_parent_id = ". $category['categories_multi']['id']
				);

				if (!empty($cat_child)){
					foreach ($cat_child as $child){
						$list_id[] = $child['categories_multi']['id'];
					}
				}
			}

			return array(
					'category' => $category,
					'list_id' => $list_id,
			);
		} catch (Exception $e) {
			throw new NotFoundException(__('Invalid category'));
		}
	}



}